package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.model.order.OrderShippingDetail;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.proxy.service.MisProxy;
import com.lsh.oms.core.redis.RedisTemplate;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Slf4j
@Component
public class OrderRoNotifyMisJop extends OmsSimpleElasticJob {

    public static String MIS_NOTIFY_RO_DETAIL_KEY = "oms:mis:notify:ro:detail";

    @Autowired
    private RoOrderService roOrderService;

    @Autowired
    private ShippingOrderService shippingService;

    @Autowired
    private MisProxy misProxy;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    protected Integer getFetchSize() {
        return 20;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.PUSH_MIS;
    }

    @Override
    protected List<Integer> dealTaskStatus() {

        List<Integer> taskStatusList = new ArrayList<>();

        taskStatusList.add(TaskStatus.TASK_UN_DEAL.getCode());
        taskStatusList.add(TaskStatus.TASK_DEAL_FAIL.getCode());

        return taskStatusList;
    }

    @Override
    protected int process(OmsTask omsTask) {

        try {
            String afsId = omsTask.getContent();
            if (StringUtils.isBlank(afsId)) {
                log.info(omsTask.getRefCode() + "【售后单号不存在】");
                return -1;
            }

            String rediskey = MIS_NOTIFY_RO_DETAIL_KEY + ":" + omsTask.getRefCode() + ":" + afsId;
            String notifyDetailRoFlag = redisTemplate.get(rediskey);
            OrderRoHead orderRoHead = this.roOrderService.getRoHeadByReturnOrderId(Long.valueOf(omsTask.getRefCode()), true);
            if (StringUtils.isBlank(notifyDetailRoFlag)) {

                OrderShippingHeadWithBLOBs orderShippingHeadWithBLOBs = this.shippingService.getBolbs(orderRoHead.getOrderId());
                List<OrderShippingDetail> shippingDetails = shippingService.getDetails(orderShippingHeadWithBLOBs.getShippingOrderId());
                JSONObject content = misProxy.notifyDetailRo(afsId, orderRoHead, orderShippingHeadWithBLOBs, shippingDetails, orderShippingHeadWithBLOBs.getVenderId());
                if (content.getInteger("ret") != 0) {
                    return -1;
                }

                redisTemplate.set(rediskey, "success");
            }

            log.info(omsTask.getContent() + "【通知 mis 返仓结果】【开始】");
            HttpMessage httpMessage = misProxy.notifyRo(afsId, orderRoHead.getVenderId());
            log.info(omsTask.getContent() + "【通知 mis 返仓结果】【结果】 {}", JSON.toJSONString(httpMessage));
            if (StringUtils.isBlank(httpMessage.getMessage())) {
                log.info(omsTask.getRefCode() + "【通知 mis 返仓结果】【失败】");
                return -1;
            }

            JSONObject messageJsonVo = JSON.parseObject(httpMessage.getMessage());
            if (messageJsonVo.getInteger("ret") == 0) {
                return 1;
            } else {
                return -1;
            }

//            log.info(omsTask.getContent() + "【通知 mis 返仓结果】【开始】");
//            HttpMessage message = misProxy.notifyFinishRo(afsId);
//            log.info(omsTask.getContent() + "【通知 mis 返仓结果】【结果】 {}", JSON.toJSONString(message));
//            if (StringUtils.isBlank(message.getMessage())) {
//                log.info(omsTask.getRefCode() + "【通知 mis 返仓结果】【失败】");
//                return false;
//            }
//
//            JSONObject messageJson = JSON.parseObject(message.getMessage());
//
//            if (messageJson.getInteger("ret") == 0) {
//                return true;
//            }else{
//                return false;
//            }

        } catch (IOException e) {
            log.error("通知mis返仓结果失败", e);
        }

        return -1;
    }
}
